package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"strings"
)

func HandelYy(forecast *models.Forecast, time string, db *gorm.DB) {
	//timeArr := strings.Split(time, "-")
	//year := timeArr[0]
	//month := timeArr[1]
	//day := timeArr[2]
	for i, report := range forecast.Reports {
		if i == 0 {
			foreList := GetForeListByCode("yy", models.Db)
			var foreDetail models.ForeDetailJson
			//获取根据time排序的最新的一条数据
			if models.Db.Where("foreListId = ? AND state != ?", foreList.Id, "0").Order("time desc").First(&foreDetail).RowsAffected != 0 {
				var forecastOld = models.Forecast{}
				json.Unmarshal(foreDetail.Data, &forecastOld)
				for j := 0; j < len(report.Data); j++ {
					if strings.Contains(report.Data[j][1].Value, "48") {
						for k := 2; k < len(report.Data[j]); k++ {
							report.Data[j][k].Value = forecastOld.Reports[0].Data[j][k].Value
						}
					} else if strings.Contains(report.Data[j][1].Value, "24") {
						for k := 2; k < len(report.Data[j]); k++ {
							report.Data[j][k].Value = forecastOld.Reports[0].Data[j+1][k].Value
						}
					}
				}
			}
			//y, m, d := tools.GetNextDay(year, month, day, 0)
			//t1 := y + "-" + m + "-" + d
			//y, m, d = tools.GetNextDay(year, month, day, 1)
			//t2 := y + "-" + m + "-" + d
			//y, m, d = tools.GetNextDay(year, month, day, 3)
			//t3 := y + "-" + m + "-" + d
			//var yyForeList []models.YyFore
			//var data [][]string
			//var yyForeList48 []models.YyFore
			//var data48 [][]string
			//if db.Where("(date = ? AND time >= ?) OR (date > ? AND date < ?) OR (date = ? AND time <= ?)", t1, "20:00", t1, t2, t2, "20:00").Find(&yyForeList).RowsAffected > 0 {
			//	for _, v := range yyForeList {
			//		valueArr := strings.Split(v.WaveValue, ",")
			//		data = append(data, valueArr)
			//	}
			//}
			//if db.Where("(date = ? AND time >= ?) OR (date > ? AND date < ?) OR (date = ? AND time <= ?)", t2, "20:00", t2, t3, t3, "20:00").Find(&yyForeList48).RowsAffected > 0 {
			//	for _, v := range yyForeList48 {
			//		valueArr := strings.Split(v.WaveValue, ",")
			//		data48 = append(data48, valueArr)
			//	}
			//}
			//
			//if len(data) == 0 {
			//	return
			//}
			////for j := 0; j < 6; j++ {
			////	report.Data[2*j+1][2].Value = data[0][j]
			////	report.Data[2*j+2][2].Value = data48[0][j]
			////}
			//for j := 0; j < 6; j++ {
			//	minFloat := math.MaxFloat64
			//	minIdx := 0
			//	maxFloat := 0.0
			//	maxIdx := 0
			//	var valueArr []float64
			//	for idx, v := range data {
			//		if v[j] != "" {
			//			value := tools.StrToFloat64(v[j])
			//			valueArr = append(valueArr, value)
			//			if value < minFloat {
			//				minFloat = value
			//				minIdx = idx
			//			}
			//			if value > maxFloat {
			//				maxFloat = value
			//				maxIdx = idx
			//			}
			//		}
			//	}
			//	minFloat = tools.Float64ToFixed(minFloat, 1)
			//	maxFloat = tools.Float64ToFixed(maxFloat, 1)
			//
			//	if maxFloat-minFloat > 0.3 {
			//		//fmt.Println(yyForeList[minIdx].Date)
			//		if minIdx < maxIdx {
			//			bw := getDayTime(yyForeList[maxIdx].Time)
			//			report.Data[2*j+1][2].Value = tools.Float64ToStr(minFloat) + "米，" + "明天" + bw + "增大到" + tools.Float64ToStr(maxFloat) + "米"
			//			//report.Data[2*j+1][3].Value = getWaveGrade(minFloat) + "到" + getWaveGrade(maxFloat)
			//			if getWaveGrade(minFloat) == getWaveGrade(maxFloat) {
			//				report.Data[2*j+1][3].Value = getWaveGrade(minFloat)
			//			} else {
			//				report.Data[2*j+1][3].Value = getWaveGrade(minFloat) + "到" + getWaveGrade(maxFloat)
			//			}
			//		} else {
			//			bw := getDayTime(yyForeList[minIdx].Time)
			//			report.Data[2*j+1][2].Value = tools.Float64ToStr(minFloat) + "米，" + "明天" + bw + "减小到" + tools.Float64ToStr(maxFloat) + "米"
			//			//report.Data[2*j+1][3].Value = getWaveGrade(maxFloat) + "到" + getWaveGrade(minFloat)
			//			if getWaveGrade(maxFloat) == getWaveGrade(minFloat) {
			//				report.Data[2*j+1][3].Value = getWaveGrade(maxFloat)
			//			} else {
			//				report.Data[2*j+1][3].Value = getWaveGrade(maxFloat) + "到" + getWaveGrade(minFloat)
			//			}
			//		}
			//	} else {
			//		report.Data[2*j+1][2].Value = tools.Float64ToFixedStr(tools.Average(valueArr), 1) + "米"
			//		report.Data[2*j+1][3].Value = getWaveGrade(tools.Average(valueArr))
			//	}
			//
			//	minFloat48 := math.MaxFloat64
			//	minIdx48 := 0
			//	maxFloat48 := 0.0
			//	maxIdx48 := 0
			//	var valueArr48 []float64
			//	for idx, v := range data48 {
			//		if v[j] != "" {
			//			value := tools.StrToFloat64(v[j])
			//			valueArr48 = append(valueArr48, value)
			//			if value < minFloat48 {
			//				minFloat48 = value
			//				minIdx48 = idx
			//			}
			//			if value > maxFloat48 {
			//				maxFloat48 = value
			//				maxIdx48 = idx
			//			}
			//		}
			//	}
			//	minFloat48 = tools.Float64ToFixed(minFloat48, 1)
			//	maxFloat48 = tools.Float64ToFixed(maxFloat48, 1)
			//
			//	if maxFloat48-minFloat48 > 0.3 {
			//		if minIdx48 < maxIdx48 {
			//			bw := getDayTime(yyForeList48[maxIdx48].Time)
			//			report.Data[2*j+2][2].Value = tools.Float64ToStr(minFloat48) + "米，" + "后天" + bw + "增大到" + tools.Float64ToStr(maxFloat48) + "米"
			//			//report.Data[2*j+2][3].Value = getWaveGrade(minFloat48) + "到" + getWaveGrade(maxFloat48)
			//			if getWaveGrade(minFloat48) == getWaveGrade(maxFloat48) {
			//				report.Data[2*j+2][3].Value = getWaveGrade(minFloat48)
			//			} else {
			//				report.Data[2*j+2][3].Value = getWaveGrade(minFloat48) + "到" + getWaveGrade(maxFloat48)
			//			}
			//		} else {
			//			bw := getDayTime(yyForeList48[minIdx48].Time)
			//			report.Data[2*j+2][2].Value = tools.Float64ToStr(minFloat48) + "米，" + "后天" + bw + "减小到" + tools.Float64ToStr(maxFloat48) + "米"
			//			if getWaveGrade(maxFloat48) == getWaveGrade(minFloat48) {
			//				report.Data[2*j+2][3].Value = getWaveGrade(maxFloat48)
			//			} else {
			//				report.Data[2*j+2][3].Value = getWaveGrade(maxFloat48) + "到" + getWaveGrade(minFloat48)
			//			}
			//		}
			//	} else {
			//		report.Data[2*j+2][2].Value = tools.Float64ToFixedStr(tools.Average(valueArr48), 1) + "米"
			//		report.Data[2*j+2][3].Value = getWaveGrade(tools.Average(valueArr48))
			//	}
			//
			//	//report.Data[2*j+1][2].Value = data[0][j]
			//	//report.Data[2*j+2][2].Value = data48[0][j]
			//}
		}
	}
}
